/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cc.wsyw126.java.lambda.java8lambda.exercise;

import cc.wsyw126.java.lambda.java8lambda.base.Artist;
import cc.wsyw126.java.lambda.java8lambda.base.Track;

import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

import java.util.Arrays;
import java.util.Comparator;

/**
 *
 * @author richard
 */
public class SteamOp {

	public static List<String> getNamesOfArtists_Lambda(List<Artist> artists) {
		return artists.stream().map(artist -> artist.getName()).collect(toList());
	}

	public static List<String> getNamesOfArtists_MethodReference(List<Artist> artists) {
		return artists.stream().map(Artist::getName).collect(toList());
	}

	public static List<Artist> artistsLivingInUK(List<Artist> artists) {
		return artists.stream().filter(artist -> "UK".equals(artist.getNationality())).collect(toList());
	}

	public static List<Artist> artistsFlatMap(List<Artist> artists1, List<Artist> artists2) {
		return Stream.of(artists1, artists2).flatMap(numbers -> numbers.stream()).collect(toList());
	}

	public static Track trackMin() {
		List<Track> tracks = Arrays.asList(new Track("Bakai", 524), new Track("Violets for Your Furs", 378),
				new Track("Time Was", 541));
		return tracks.stream().min(Comparator.comparing(track -> track.getLength())).get();
	}

	public static int computeReduce() {
		int sum = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
		return sum;

	}
}
